<template>
  <div class="app-container">
    <el-button type="primary" style="margin-bottom: 1.5rem" @click="addBannerItem">新增首页预览图</el-button>
    <!-- 数据表格 -->
    <el-table :data="data" style="width: 100%" border>
      <el-table-column prop="date" label="序号" width="60" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="150" align="center">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column prop="description" label="描述" />
      <el-table-column label="中图预览" align="center">
        <template slot-scope="scope">
          <el-image style="width: 100px" :src="scope.row.midImg2" fit="fill" />
        </template>
      </el-table-column>
      <el-table-column label="大图预览" align="center">
        <template slot-scope="scope">
          <el-image style="width: 100px" :src="scope.row.bigImg2" fit="fill" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top" :hide-after="2000">
            <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="editBannerHandle(scope.row)" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top" :hide-after="2000">
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteBannerHandle(scope.row)" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑首页标语 -->
    <el-dialog title="请编辑信息" :visible.sync="dialogFormVisible" top="5vh">
      <el-form :model="form">
        <!-- 标题 -->
        <el-form-item label="标题">
          <el-input v-model="form.title" />
        </el-form-item>

        <!-- 描述 -->
        <el-form-item label="描述">
          <el-input v-model="form.description" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="首页中图">
              <!-- 中图 -->
              <Upload v-model="form.midImg" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页大图">
              <!-- 大图 -->
              <Upload v-model="form.bigImg" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="激活状态">
            <el-switch v-model="form.active" :value="data.active" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBannerConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBanner, setBanner } from '@/api/banner.js'
import { server_URL } from '@/urlConfig.js'
// 引入封装好的上传图片组件
import Upload from '@/components/Upload.vue'

export default {
  components: {
    Upload
  },
  data() {
    return {
      data: [], // 存储数据
      dialogFormVisible: false, // 编辑对话框是否显示
      form: {
        id: '',
        midImg: '',
        bigImg: '',
        title: '',
        description: '',
        active: false
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getBanner().then((res) => {
        this.data = res.data
        console.log(res.data, '获取数据')
        for (var item of this.data) {
          item.midImg2 = server_URL + item.midImg
          item.bigImg2 = server_URL + item.bigImg
        }
      })
    },
    editBannerHandle(bannerInfo) {
      // 1. 将 bannerInfo 的数据给 form 2. 打开 dialog
      this.form = { ...bannerInfo }
      console.log(this.form, 'form')
      this.dialogFormVisible = true
    },
    deleteBannerHandle(bannerInfo) {
      this.form = { ...bannerInfo }
      console.log(this.form, 'this.form')
      // this.data.
      this.$confirm('删除该首页配图，是否继续?', '是否删除此篇文章', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delOnebanner(this.form.id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editBannerConfirm() {
      // 从表单里面拿到用户修改的数据，发送给服务器
      // 因为 api 文档要求三个首页标语都要发送过去，哪怕只改了其中一个
      var arr = [...this.data]
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].id === this.form.id) {
          console.log(this.form.id, 'form.id')
          arr[i] = this.form
        }
        if (!arr[i].id) {
          arr[i].midImg = this.form.midImg || ''
          arr[i].bigImg = this.form.bigImg || ''
          arr[i].title = this.form.title || ''
          arr[i].description = this.form.description || ''
          arr[i].active = this.form.active || false
        }
      }
      console.log(arr, '上送的arr')
      setBanner(arr).then((res) => {
        this.dialogFormVisible = false // 关闭掉对话框
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.fetchData()
      })
    },
    addBannerItem() {
      this.data.push({})
    },
    delOnebanner(id) {
      const newDate = this.data.filter((item) => item.id !== id)
      setBanner(newDate).then((res) => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.fetchData()
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
